package com.mrent.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.springframework.stereotype.Controller;

import com.mrent.entity.GoodsOrderInfo;

@Controller
public class GoodsOrderServiceImpl implements GoodsOrderService {
	
	/**
	 * 保存商品订单.
	 * 
	 * @param goodsOrder
	 */
	public void saveGoodsOrder(GoodsOrderInfo goodsOrder){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try{
			trans.begin();
			em.persist(goodsOrder);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			trans.commit();
		}
	}
	
	/**
	 * 获取订单信息.
	 * 
	 * @param orderNo
	 * @return
	 */
	public GoodsOrderInfo getGoodsOrder(String orderNo){
		EntityManager em = EMF.getEM();
		GoodsOrderInfo goodsOrder = null;
		try{
			String jpql = "select from GoodsOrderInfo goodsOrder where goodsOrder.orderSn=:orderNo";
			Query query = em.createQuery(jpql);
			query.setParameter("orderNo", orderNo);
			goodsOrder = (GoodsOrderInfo)query.getSingleResult();
		}catch(Exception e){
			e.printStackTrace();
		}
		return goodsOrder;
	}
	
	/**
	 * 获取用户订单.
	 * 
	 * @param memberName
	 * @return
	 */
	public List<GoodsOrderInfo> getMemberOrder(String memberName){
		EntityManager em = EMF.getEM();
		String jql = "select from GoodsOrderInfo goodsOrder where goodsOrder.userName=:userName";
		Query query = em.createQuery(jql);
		query.setParameter("userName", memberName);
		return query.getResultList();
	}

}
